import entity.Goods;
import entity.Order;
import entity.User;
import service.GoodsManagement;
import util.Result;

import java.util.Arrays;
import java.util.Scanner;


public class Test2 {
    private static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        GoodsManagement gm = new GoodsManagement();

        test_add(gm);
        //test_remove(gm);
        //test_findGoodsById(gm);
        //test_findByType(gm);
        //test_storeLessThan10(gm);
        //test_getMaxGoods(gm);

        test_sellGoods(gm);
        test_findOrderById(gm);
    }

    /**
     * 根据订单编号查询订单信息
     */
    public static void test_findOrderById(GoodsManagement gm){
        System.out.println("请输入订单编号");
        long orderId = input.nextLong();
        Result result = gm.findOrderById(orderId);
        if (result.getCode() != 0) {
            System.out.println(result.getNessage());
        } else {
            Order order = (Order) result.getData();
            System.out.println(order);
        }
    }

    /**
     * 销售商品

     */
    public static void test_sellGoods(GoodsManagement gm){

        System.out.println("开始购买商品,请输入要购买的商品种类数量");
        int goods_count = input.nextInt();
        Goods[] sellGoods = new Goods[goods_count];
        int index = 0;
        while (true) {
            System.out.println("请输入商品编号，输入负数停止添加");
            int goodsId = input.nextInt();
            if (goodsId < 0) {
                System.out.println("结束添加商品");
                return;
            }
            //根据编号查询商品
            Result result = gm.findGoodsById(goodsId);
            if (result.getCode() == 0) {
                Goods goods = (Goods) result.getData();
                System.out.println("请输入购买数量");
                int count = input.nextInt();
                if (goods.getCount() < count) {
                    System.out.println("库存不足，请重新输入");
                }else{
                    goods.setCount(count);
                    sellGoods[index++] = goods;
                    if (index >= sellGoods.length) {
                        break;
                    }
                }
            } else {
                System.out.println(result.getNessage());
            }
        }

        //购买的逻辑
        Result result = gm.sellGoods(sellGoods);
        if (result.getCode() != 0) {
            System.out.println(result.getNessage());
        } else {
            Order order = (Order) result.getData();
            System.out.println(order);
        }
    }

    /**
     * 统计最贵的商品信息
     */
    public static void test_getMaxGoods(GoodsManagement gm){
        Result result = gm.findByMaxSellPrice();
        if (result.getCode() != 0) {
            System.out.println(result.getNessage());
        } else {
            Goods[] $goods = (Goods[]) result.getData();
            System.out.println(Arrays.toString($goods));
        }
    }


    /**
     * 查询库存不足10的商品信息
     *
     */
    public static void test_storeLessThan10(GoodsManagement gm){
        Result result = gm.storeLessThan10();
        if (result.getCode() != 0) {
            System.out.println(result.getNessage());
        } else {
            Goods[] $goods = (Goods[]) result.getData();
            System.out.println(Arrays.toString($goods));
        }
    }

    /**
     * 测试根据id查询商品
     */
    public static void test_findByType(GoodsManagement gm){
        System.out.println("请输入商品类型");
        String type = input.next();
        Result result = gm.findByType(type);
        if (result.getCode() != 0) {
            System.out.println(result.getNessage());
        } else {
            //取出来的数据也是一个数组
            Object data = result.getData();
            //强制向下类型转换
            Goods[] $goods = (Goods[]) data;
            System.out.println(Arrays.toString($goods));
        }
    }

    /**
     * 测试根据编号查询商品
     */
    public static void test_findGoodsById(GoodsManagement gm){
        System.out.println("请输入订单编号");
        long goodsId = input.nextLong();
        Result result = gm.findGoodsById(goodsId);
        if (result.getCode() != 0) {
            System.out.println(result.getNessage());
        } else {
            Object data = result.getData();
            Goods goods = (Goods) data; //将Object类型的对象强制转换为Goods类型
            System.out.println(goods);
        }
    }
    public static void test_remove(GoodsManagement gm){
        System.out.println("请输入订单编号");

        long goodsId = input.nextLong();//测试，编号是写死的。将来的编号是用户输入的
        Result result = gm.removeById(goodsId);
        System.out.println(result);
    }

    public static void test_add(GoodsManagement gm){
        /*Goods g1 = new Goods();
        g1.setName("牛肉干");
        g1.setType("食品");
        g1.setPrice(20);
        g1.setShellPrice(45);
        g1.setCount(10);

        Result result = gm.addOrUpdateGoods(g1);//第一次添加
        System.out.println(result);

        Goods g2 = new Goods();
        g2.setName("豆腐干");
        g2.setType("食品");
        g2.setPrice(10);
        g2.setShellPrice(35);
        g2.setCount(5);

        result = gm.addOrUpdateGoods(g2); //第二次是更新

        System.out.println(result);

        Goods g3 = new Goods();
        g3.setName("黑人牙膏");
        g3.setType("日用品");
        g3.setPrice(10);
        g3.setShellPrice(45);
        g3.setCount(5);
        Result result1 = gm.addOrUpdateGoods(g3);
        System.out.println(result1);*/
        System.out.println("请输入商品名称");
        String name = input.next();
        System.out.println("请输入商品类型");
        String type = input.next();
        System.out.println("请输入商品入库价格");
        double price = input.nextDouble();
        System.out.println("请输入商品销售价格");
        double sellPrice = input.nextDouble();
        System.out.println("请输入商品入库数量");
        int count = input.nextInt();

        Goods goods = new Goods();
        goods.setName(name);
        goods.setType(type);
        goods.setPrice(price);
        goods.setShellPrice(sellPrice);
        goods.setCount(count);

        Result result1 = gm.addOrUpdateGoods(goods);
        System.out.println(result1);
    }
}